package jhh.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

/**
 * Servlet implementation class UploadwithServlet
 */
@WebServlet(name="UploadwithServlet",urlPatterns="/UploadwithServlet")
public class UploadwithServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       ServletConfig servletconfig;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UploadwithServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    public void init(ServletConfig config){
    	this.servletconfig=config;
    }
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//创建一个新的SmartUpload对象
		response.setContentType("text/html;charset=utf-8");
		SmartUpload su=new SmartUpload();
		//上传初始化
		su.initialize(servletconfig, request, response);
		//限制上传数据的长度
		su.setMaxFileSize(100000);
		//设定允许上传的文件（通过扩展名限制），仅允许jmx文件
		
		//设定禁止上传的文件（通过扩展名限制），禁止上传带有EXE，jsp，bat，htm,html为后缀名和没有后缀名的文件
		try {
			su.upload();
		} catch (Exception e) {
			// TODO: handle exception
		}
		//上传文件
		
		//将上传文件全部保存到指定目录
		try {
			int count=su.save("/upload");
		} catch (SmartUploadException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
}
